package com.hsh.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hsh.bean.*;
import com.hsh.service.SleepService;
import com.hsh.service.UserService;
import com.hsh.util.DateFormatUtil;
import com.hsh.util.FloatDecimalFormatUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserRestController {

    @Resource
    private UserService userService;

    @Resource
    private SleepService sleepService;

    /**
     * 用户信息分页显示
     *
     * @param offset
     * @param limit
     * @param user
     * @return
     */
    @RequestMapping("/list-data")
    public String list(@RequestParam(defaultValue = "0") int offset,
                       @RequestParam(defaultValue = "5") int limit,
                       User user) {
        if (user.getUsername() == "") {
            user.setUsername(null);
        }
        if (user.getTel() == "") {
            user.setTel(null);
        }
        //分页查询
        PageInfo<User> userPageInfo = userService.findUserByPage(user, offset / limit + 1, limit);
        //将查询结果封装到ResultData中显示
        ResultData<User> resultData = new ResultData<>();
        resultData.setTotal(userPageInfo.getTotal());
        resultData.setRows(userPageInfo.getList());
        //将结果封装成JSON格式
        String json = JSON.toJSONString(resultData);
        return json;
    }

    @RequestMapping("/plan-data")
    public String plan(Integer uid) {
        List<Plan> plans = new ArrayList<>();

        Date today = new Date();
        Sleep s = new Sleep();
        s.setUid(1);
        s.setSDate(today);
        System.out.println("uid=1&sDate=today" + sleepService.findByDateAndUid(s));

        Float totalCycle = 0f;
        for (int frontDay = 1; frontDay <= 7; frontDay++) {
            Sleep sleep = new Sleep();
            sleep.setUid(uid);
            //将前X天存在sType中
            sleep.setSType(frontDay);
//            Date date = DateFormatUtil.cutDate(today, frontDay);
//            sleep.setSDate(date);
////            sleep.setSDate(date);
//            System.out.println(sleep);
            //X天前的睡眠周期总数
            Float theDayCycle =
                    sleepService.findByDateAndUid(sleep) == null ? 0 :
                            Float.valueOf(sleepService.findByDateAndUid(sleep));
            //前X天的睡眠周期总数
            totalCycle += theDayCycle;
            //前X天的平均周期数
            String avgCycle = FloatDecimalFormatUtil.decimalFormat(totalCycle / frontDay);
            String afterPlanMinTime = null;
            String afterPlanMaxTime = null;
            if (frontDay < 7) {
                //计划的最小睡眠时间
                afterPlanMinTime = FloatDecimalFormatUtil.decimalFormat((30 - totalCycle) / (7 - frontDay));
                //计划的最小睡眠时间
                afterPlanMaxTime = FloatDecimalFormatUtil.decimalFormat((35 - totalCycle) / (7 - frontDay));
            }
            Plan plan = new Plan(frontDay, avgCycle, afterPlanMinTime, afterPlanMaxTime);
            System.out.println("userRestController-----plan-data");
            System.out.println(totalCycle);
            System.out.println(plan);
            plans.add(plan);
        }
        return JSON.toJSONString(plans);
    }

    @RequestMapping("/delete-data")
    public String delete(Integer uid) {
        Integer delete = userService.delete(uid);
        Message msg = new Message();
        msg.setState(delete - 1);
        msg.setResult(delete == 1 ? "删除成功" : "删除失败");
        return JSON.toJSONString(msg);
    }

}
